﻿using System;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using PlanetTelex.RssViewer.Models;
using PlanetTelex.RssViewer.Services;

namespace PlanetTelex.RssViewer.Drivers
{
    public class RssViewerDriver : ContentPartDriver<RssViewerPart>
    {
        private readonly IRssViewerService _rssAggregatorService;

        public RssViewerDriver(IRssViewerService rssAggregatorService)
        {
            _rssAggregatorService = rssAggregatorService;
        }

        protected override DriverResult Display(RssViewerPart part, string displayType, dynamic shapeHelper)
        {
            if (!string.IsNullOrWhiteSpace(part.FeedUrl))
            {
                return ContentShape("Parts_RssViewer", () => shapeHelper.Parts_RssViewer(
                    FeedUrl: part.FeedUrl,
                    ItemsToDisplay: part.ItemsToDisplay,
                    Feed: _rssAggregatorService.GetFeed(part),
                    ContentPart: part,
                    ContentItem: part.ContentItem
                    ));
            }
            return new DriverResult();
        }

        /// <summary>
        /// Editors the specified part.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="shapeHelper">The shape helper.</param>
        /// <returns></returns>
        protected override DriverResult Editor(RssViewerPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_RssViewer_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/RssViewer", Model: part, Prefix: Prefix));
        }

        /// <summary>
        /// The Editor POST Driver.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="updater">The updater.</param>
        /// <param name="shapeHelper">The shape helper.</param>
        protected override DriverResult Editor(RssViewerPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            if (part == null || updater == null)
                return Editor(null, shapeHelper);

            updater.TryUpdateModel(part, Prefix, null, null);

            return Editor(part, shapeHelper);
        }

        /// <summary>
        /// Exportings the specified part.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="context">The context.</param>
        protected override void Exporting(RssViewerPart part, ExportContentContext context)
        {
            context.Element(part.PartDefinition.Name).SetAttributeValue("CacheDuration", part.CacheDuration);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ItemsToDisplay", part.ItemsToDisplay);
            context.Element(part.PartDefinition.Name).SetAttributeValue("FeedUrl", part.FeedUrl);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowTitleInHeader", part.ShowTitleInHeader);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowAuthorInHeader", part.ShowAuthorInHeader);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowDomainInHeader", part.ShowDomainInHeader);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowTitleInFooter", part.ShowTitleInFooter);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowAuthorInFooter", part.ShowAuthorInFooter);
            context.Element(part.PartDefinition.Name).SetAttributeValue("ShowDomainInFooter", part.ShowDomainInFooter);
            context.Element(part.PartDefinition.Name).SetAttributeValue("LinkTitle", part.LinkTitle);
            context.Element(part.PartDefinition.Name).SetAttributeValue("LinkDomain", part.LinkDomain);
        }

        /// <summary>
        /// Importings the specified part.
        /// </summary>
        /// <param name="part">The part.</param>
        /// <param name="context">The context.</param>
        protected override void Importing(RssViewerPart part, ImportContentContext context)
        {
            part.CacheDuration = Convert.ToInt32(context.Attribute(part.PartDefinition.Name, "CacheDuration"));
            part.ItemsToDisplay = Convert.ToInt32(context.Attribute(part.PartDefinition.Name, "ItemsToDisplay"));
            part.FeedUrl = context.Attribute(part.PartDefinition.Name, "FeedUrl");
            part.ShowTitleInHeader = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowTitleInHeader"));
            part.ShowAuthorInHeader = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowAuthorInHeader"));
            part.ShowDomainInHeader = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowDomainInHeader"));
            part.ShowTitleInFooter = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowTitleInFooter"));
            part.ShowAuthorInFooter = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowAuthorInFooter"));
            part.ShowDomainInFooter = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "ShowDomainInFooter"));
            part.LinkTitle = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "LinkTitle"));
            part.LinkDomain = Convert.ToBoolean(context.Attribute(part.PartDefinition.Name, "LinkDomain"));
        }
    }
}